package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.LogisticsAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerLogisticsComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.LogisticsModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.LogisticsModel;
import com.zw.yzk.kabu.presenter.LogisticsPresenter;
import com.zw.yzk.kabu.view.view.LogisticsView;


import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/3/18.
 */

public class LogisticsActivity extends BaseActivity implements LogisticsView {

    @Bind(R.id.logistics_id)
    TextView logisticsId;
    @Bind(R.id.logistics)
    RecyclerView logistics;
    @Bind(R.id.empty)
    View empty;

    @Inject
    LogisticsPresenter presenter;
    private LogisticsAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_logistics;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.logistics))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerLogisticsComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .logisticsModule(new LogisticsModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        logistics.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        getLogisticInfo();
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getLogisticsSuccess(List<LogisticsModel> list) {
        if (adapter == null) {
            adapter = new LogisticsAdapter(list);
            logistics.setAdapter(adapter);
        } else {
            adapter.refresh(list);
        }

        showEmptyHint(adapter.getItemCount() == 0);
    }

    @Override
    public void setLogisticId(String id) {
        logisticsId.setText(getResources().getString(R.string.logistics_code, id));
    }

    /**
     * 是否显示空提示
     *
     * @param show true：显示，false：不显示
     */
    private void showEmptyHint(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取物流信息
     */
    private void getLogisticInfo() {
        String id = getIntent().getStringExtra(ActivityConstant.LOGISTICS_ID);
        presenter.getLogisticsInfo(id);
    }
}
